package com.example.cofeed.activity;

import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;


import com.ada.common.util.GsonUtil;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.PhoneUtils;
import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;
import com.example.cofeed.constant.SputilsConstant;
import com.example.cofeed.entity.User;
import com.example.cofeed.even.LoginEeven;
import com.lzy.okgo.model.Response;
import com.example.cofeed.MainActivity;
import com.example.cofeed.R;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.base.BaseActivity;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.constant.Constants;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mqwang on 2017/9/15.
 */

public class LoginActivity extends BaseActivity {

    @BindView(R.id.phone_et)
    EditText phoneEt;
    @BindView(R.id.pwd_et)
    EditText pwdEt;
    @BindView(R.id.pwd_visible_iv)
    ImageView pwdVisibleIv;

    boolean isVisible;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
        LogUtils.d("hello");
        BarUtils.setStatusBarColor(this, getResources().getColor(R.color.colorWhite));

    }

    @Override
    protected boolean isNeedTitleBar() {
        return false;
    }

    @OnClick({R.id.login_tv, R.id.register_tv, R.id.forget_tv, R.id.pwd_visible_iv, R.id.iv_close, R.id.phone_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_tv:

                String phone = phoneEt.getText().toString().trim();
                String pwd = pwdEt.getText().toString().trim();
                if (ObjectUtils.isEmpty(phone)) {
                    showToast("请输入用户名");
                    return;
                }
                if (ObjectUtils.isEmpty(pwd)) {
                    showToast("请输入密码");
                    return;
                }

                SPUtils.getInstance().put(SputilsConstant.TOKEN, "");
                SPUtils.getInstance().put(SputilsConstant.AUTO_TOKEN, "");

                ApiService.login(this, phone, pwd, new DialogCallback<LzyResponse<Map<String, String>>>(this) {
                    @Override
                    public void onSuccess(Response<LzyResponse<Map<String, String>>> response) {
                        String accesstoken = response.body().data.get("accesstoken");
                        String name = response.body().data.get("name");
                        String picture = response.body().data.get("picture");
//                        int isfirst = Integer.parseInt(response.body().data.get("isfirst"));

                        SPUtils.getInstance().put(SputilsConstant.TOKEN, accesstoken);
                        SPUtils.getInstance().put(SputilsConstant.NAME, name);
                        SPUtils.getInstance().put(SputilsConstant.PICTURE, picture);

                        ApiService.sms_autologin("sms_autologin", new DialogCallback<LzyResponse<Map<String, String>>>(LoginActivity.this) {
                            @Override
                            public void onSuccess(Response<LzyResponse<Map<String, String>>> response) {
                                ActivityUtils.startActivity(LoginActivity.this, MainActivity.class);
                                String smstoken = response.body().data.get("smstoken");
                                SPUtils.getInstance().put(SputilsConstant.AUTO_TOKEN, smstoken);
                                EventBus.getDefault().post(new LoginEeven());
                                finish();
                                KeyboardUtils.hideSoftInput(view);

                            }

                            @Override
                            public void onError(Response<LzyResponse<Map<String, String>>> response) {
                                super.onError(response);
                                showToast("登录失败");

                            }
                        });
                    }
                });
                break;
            case R.id.register_tv:
                ActivityUtils.startActivity(this, RegisterStepPhoneActivity.class);
                break;
            case R.id.forget_tv:
                ActivityUtils.startActivity(this, InputPhoneActivity.class);
                break;
            case R.id.pwd_visible_iv:
                if (isVisible) {
                    pwdVisibleIv.setImageResource(R.mipmap.icon_login_eyes_normal);
                    isVisible = false;
                    pwdEt.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    pwdVisibleIv.setImageResource(R.mipmap.icon_login_eyes_click);
                    isVisible = true;
                    pwdEt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                break;
            case R.id.iv_close:
                finish();
                KeyboardUtils.hideSoftInput(view);
                break;
            case R.id.phone_tv:
                ApiService.service_phone(this, new DialogCallback<LzyResponse<Map<String, String>>>(this) {
                    @Override
                    public void onSuccess(Response<LzyResponse<Map<String, String>>> response) {
                        String tel = response.body().data.get("tel");
                        PhoneUtils.dial(tel);

                    }
                });
                break;
            default:
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
